package com.efounder.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.bean.CommentDatas;
import com.efounder.mobilemall.R;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.utilcode.util.ConvertUtils;
import com.utilcode.util.ScreenUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by wei on 2018/2/28/028.
 */

public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {

    private View footView;
    public static final int NORMAL = 1;
    public static final int FOOT = 2;
    private Context mContext;
    private List<CommentDatas.Comments> commentDatas;
    private int index;
    private GlideImageLoader glideImageLoader;

    public CommentsAdapter(Context context, List<CommentDatas.Comments> datats, int position) {
        mContext = context;
        commentDatas = datats;
        index = position;
        glideImageLoader = new GlideImageLoader();
    }

    public void addFootView(View footView) {
        this.footView = footView;
    }

    public int getFootViewCount() {
        return footView == null ? 0 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position >= commentDatas.size()) {
            return FOOT;
        }
        return NORMAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == FOOT) {
            View viewFoot = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_foot_comments, parent, false);
            return new ViewHolder(viewFoot);
        } else {
            // 实例化展示的view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_comments, parent, false);
            // 实例化viewholder
            return new ViewHolder(v);
        }

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position == 0) {
            position = index;
        } else if (position < index + 1) {
            position--;
        }
        if (getItemViewType(position) == NORMAL && position < commentDatas.size()) {
            if (commentDatas.get(position).getCommentPics().size() > 0) {
                holder.image_rv.setVisibility(View.VISIBLE);

                ArrayList<ImageInfo> imageInfo = new ArrayList<>();
                for (int i = 0; i < commentDatas.get(position).getCommentPics().size(); i++) {
                    ImageInfo info = new ImageInfo();
                    info.setThumbnailUrl(commentDatas.get(position).getCommentPics().get(i).getCommentPicUrl());
                    info.setBigImageUrl(commentDatas.get(position).getCommentPics().get(i).getCommentPicUrl());
                    imageInfo.add(info);
                }
                NineGridViewClickAdapter nineGridViewClickAdapter = new NineGridViewClickAdapter(mContext, imageInfo);
                holder.image_rv.setAdapter(nineGridViewClickAdapter);
//                ScrollAdapter mAdapter = new ScrollAdapter(commentDatas.get(position).getCommentPics(), R.layout.item_comments_scroll, mContext);
//                //设置布局管理器
//                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
//                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//                holder.image_rv.setLayoutManager(linearLayoutManager);
//                holder.image_rv.setAdapter(mAdapter);
            } else {
                holder.image_rv.setVisibility(View.GONE);
//                holder.tvFootView.setText("还没有评论");
            }

            // 绑定数据
            Glide.with(mContext)
                    .load(commentDatas.get(position).getUserAvatar())
                    .apply(new RequestOptions().transform(new CircleCrop()))
                    .into(holder.userAvatar);
//            Glide.with(mContext).load(commentDatas.get(position).getUserAvatar()).into(holder.userAvatar);
            holder.userName.setText(commentDatas.get(position).getUserName());
            holder.textComments.setText(commentDatas.get(position).getContent());
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
                    format(new Date(Long.valueOf(commentDatas.get(position).getTime())));
            holder.dateComments.setText(date);
        } else if (commentDatas.size() == 0) {
            holder.tvFootView.setText(R.string.mail_no_comment);
        }

    }

    private String longToString(long date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
        return sdf.format(new Date(Long.parseLong(String.valueOf(date)))).substring(0, 10);
    }

    @Override
    public int getItemCount() {
        return commentDatas.size() + getFootViewCount();
    }


    private static class GlideImageLoader implements NineGridView.ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
//            Picasso.with(context).load(url)//
////                    .placeholder(R.drawable.ic_default_image)//
////                    .error(R.drawable.ic_default_image)//
//                    .into(imageView);
            com.efounder.imageloader.GlideImageLoader.getInstance().displayImage(context, imageView, url);

        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView userAvatar;
        TextView userName;
        TextView userInfo;
        TextView textComments;
        TextView dateComments;
        TextView tvFootView;
        NineGridView image_rv;


        public ViewHolder(View itemView) {
            super(itemView);
//            if (itemView == footView) {
            tvFootView = itemView.findViewById(R.id.footer);
//            } else {
            userAvatar = (ImageView) itemView.findViewById(R.id.user_avatar);
            userName = itemView.findViewById(R.id.user_name);
//                userInfo = itemView.findViewById(R.id.user_info);
            textComments = itemView.findViewById(R.id.text_comments);
            dateComments = itemView.findViewById(R.id.date_comments);
            image_rv = itemView.findViewById(R.id.image_rv);
            //设置只有一张图片时的大小
            image_rv.setSingleImageSize((ScreenUtils.getScreenWidth()- ConvertUtils.dp2px(30))/3);
            NineGridView.setImageLoader(glideImageLoader);
//            }

        }
    }
}
